package com.dcm.groupbuy.controller;

import com.dcm.common.annotation.EnableResponseBodyAdvice;
import com.dcm.common.vo.CommonResponse;
import com.dcm.groupbuy.cilent.ItemClient;
import com.dcm.groupbuy.entity.GroupBuyEntity;
import com.dcm.groupbuy.entity.GroupOrderEntity;
import com.dcm.groupbuy.service.GroupBuyService;
import com.dcm.groupbuy.vo.ItemVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 牛浩
 */
@Api(tags = "GroutBuy")
@RestController
@CrossOrigin
@RequestMapping("/groupBuy")
@EnableResponseBodyAdvice
public class GroutBuyController {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private GroupBuyService groupBuyService;

    /**
     * 查询全部
     * @return
     */
    @ApiOperation("查询全部")
    @GetMapping("/findAll")
    public List<GroupBuyEntity> findAll(){
        return groupBuyService.findAll();
    }

    /**
     * 根据id删除
     * @param id
     */
    @ApiOperation("根据id删除")
    @DeleteMapping("/delete")
    public void delete(@RequestParam(value = "id",required = false) Long id){
        groupBuyService.delete(id);
    }

    /**
     * 添加
     * @param groupBuyEntity
     */
    @ApiOperation("添加")
    @PostMapping("/insertGroupBuy")
    public void insertGroupBuy(@RequestBody GroupBuyEntity groupBuyEntity){
        groupBuyService.insertGroupBuy(groupBuyEntity);
    }

    /**
     * 修改
     * @param groupBuyEntity
     */
    @ApiOperation("修改")
    @PutMapping("/updateGroupBuy")
    public void updateGroupBuy(@RequestBody GroupBuyEntity groupBuyEntity){
        groupBuyService.updateGroupBuy(groupBuyEntity);
    }

    /**
     * 模糊查询
     * @param groupState
     * @param groupName
     * @return
     */
    @ApiOperation("模糊查询")
    @GetMapping("/search")
    public List<GroupBuyEntity> search(
            @RequestParam(value = "groupState",required = false) Integer groupState,
            @RequestParam(value = "groupName",required = false) String groupName
    ){
        return groupBuyService.search(groupState,groupName);
    }

    /**
     * 远程调用item查询所有方法
     * @return
     */
    @ApiOperation("远程调用item查询所有方法")
    @GetMapping("/findItemAll")
    public CommonResponse findItemAll(){
        return itemClient.findAll();
    }

    /**
     * 远程调用item根据id查询
     * @param id
     * @return
     */
    @ApiOperation("远程调用item根据id查询")
    @GetMapping("/findItemById")
    public CommonResponse<ItemVo> findItemById(@RequestParam(value = "id",required = false) Long id){
        return itemClient.findItemById(id);
    }
    @ApiOperation("分页")
    @GetMapping("/findPage")
    public PageInfo<GroupBuyEntity> findPage(@RequestParam(value = "currentPage", required = false) Integer currentPage,
                                               @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        return groupBuyService.findPage(currentPage, pageSize);
    }
}
